var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    userInfo: {},
    villageList: [],
    villageIndex: 999,
    address: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id || ''
    })
    this.water_id = options.water_id;
    wx.showLoading()
    this.getInfo();
  },



  /**
   * 获取申请信息
   */
  getInfo: function () {
    var that = this;
    let param = {};
    if (that.water_id) {
      param.water_id = that.water_id;
    } else {
      param.id = that.data.id
    }
    app.ajax({
      url: 'Partner/WaterCard/getWaterCardAudit',
      data: param,
      success: function (res) {
        //console.log(res)
        wx.hideLoading();
        if (res.data.code == 1000) {
          that.getVillage(res.data.data.village_id);
          that.setData({
            userInfo: res.data.data
          })
        } else {

        }
      }
    })
  },

  // 选择园区
  communityChange: function (e) {
    let i = e.detail.value,
      villageList = this.data.villageList,
      address = this.data.address;
    if (villageList[i].province_name == villageList[i].city_name) {
      address = villageList[i].province_name + villageList[i].area_name + villageList[i].address
    } else {
      address = villageList[i].province_name + villageList[i].city_name + villageList[i].area_name + villageList[i].address
    }

    this.setData({
      villageIndex: i,
      address: address
    })
  },

  // 确认
  formSubmit: function (e) {
    var that = this;
    let info = e.detail.value;
    if (info.villageIndex === 999) {
      app.showToast("请选择所属园区");
      return;
    }
    let province = that.data.villageList[info.villageIndex].province,
      area = that.data.villageList[info.villageIndex].area,
      city = that.data.villageList[info.villageIndex].city,
      village_id = that.data.villageList[info.villageIndex].id,
      village_name = that.data.villageList[info.villageIndex].village_name;
    let salesman_id = wx.getStorageSync('salesman_id');

    app.ajax({
      url: 'Partner/WaterCard/editWaterCardAudit',
      data: {
        village_name: village_name,
        card_number: that.data.userInfo.card_number,
        full_name: info.fullName,
        tel: info.tel,
        IDcard: info.IDcard,
        address: info.address,
        province: province,
        city: city,
        area: area,
        id: that.data.userInfo.id,
        village_id: village_id,
        salesman_id: salesman_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          app.showToast(res.data.message, "success");
          let pages = getCurrentPages();
          let targetPage = pages[pages.length - 2];
          setTimeout(() => {
            if (that.water_id || that.data.id) {
              targetPage.reloadData();
            }
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        } else {
          app.showToast(res.data.message)
        }
      }
    })
  },
  //获取园区列表
  getVillage(village_id) {
    var that = this;
    app.ajax({
      url: 'Salesman/Card/getPartnerVillage',
      data: {
        village_name: ''
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          let villageList = res.data.data;
          let villageIndex = that.data.villageIndex;
          let address = that.data.address;
          for (let i = 0; i < villageList.length; i++) {
            if (village_id && village_id == villageList[i].id) {
              villageIndex = i
              if (villageList[i].province_name == villageList[i].city_name) {
                address = villageList[i].province_name + villageList[i].area_name + villageList[i].address
              } else {
                address = villageList[i].province_name + villageList[i].city_name + villageList[i].area_name + villageList[i].address
              }
            }
          }
          that.setData({
            villageList: villageList,
            villageIndex: villageIndex,
            address: address
          })
        } else {

        }
      }
    })
  }
})